package com.hlkj.baojin;

import com.hlkj.baojin.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.unit.DataSize;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * 报警管控系统
 */
@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableSwagger2
public class BaojinApplication {

    @Value("${filetemp.windows}")
    private String windowsFilepath;

    @Value("${filetemp.others}")
    private String othersFilepath;

    public static void main(String[] args) {
        SpringApplication.run(BaojinApplication.class, args);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大
        factory.setMaxFileSize(DataSize.ofMegabytes(200)); //KB,MB 2.1:KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(200));

        String location;
        if(System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
            location = windowsFilepath;
        } else {
            location = othersFilepath;
        }
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);

        return factory.createMultipartConfig();
    }
    @Bean
    MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

}

